package com.example.qrcodecamera;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import androidx.core.content.ContextCompat;
import com.journeyapps.barcodescanner.ViewfinderView;

public class CustomViewfinderView extends ViewfinderView {
    private Rect framingRect; // 扫描框区域
    private Paint borderPaint; // 自定义边框画笔

    public CustomViewfinderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    // 初始化画笔
    private void init() {
        borderPaint = new Paint();
        borderPaint.setColor(ContextCompat.getColor(getContext(), R.color.zxing_viewfinder_border));
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setStrokeWidth(4f); // 边框宽度
    }

    // 设置扫描框区域
    public void setFramingRect(Rect rect) {
        this.framingRect = rect;
        invalidate(); // 触发重绘
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas); // 保留父类默认绘制逻辑（如扫描线动画）
        if (framingRect != null) {
            // 绘制自定义边框
            canvas.drawRect(
                    framingRect.left,
                    framingRect.top,
                    framingRect.right,
                    framingRect.bottom,
                    borderPaint
            );
        }
    }
}